package cz.muni.fi.pa165.soccergamesarchive.persistencelayer.entities;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;

/**
 *
 * @author Tomas Plevko <tomasplevko@gmail.com>
 */
@Entity
@NamedQuery(name = "Goal.getAllGoals", query = "SELECT g FROM Goal g")
public class Goal implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private int scoreTime;

    @ManyToOne
    @JoinColumn(nullable = false)
    private SoccerMatch soccerMatch;

    @ManyToOne
    @OnDelete(action = OnDeleteAction.CASCADE)
    @JoinColumn(nullable = false)
    private Player player;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public int getScoreTime() {
        return scoreTime;
    }

    public void setScoreTime(int scoreTime) {
        this.scoreTime = scoreTime;
    }

    public SoccerMatch getSoccerMatch() {
        return soccerMatch;
    }

    public void setSoccerMatch(SoccerMatch soccerMatch) {
        this.soccerMatch = soccerMatch;
    }

    public Player getPlayer() {
        return player;
    }

    public void setPlayer(Player player) {
        this.player = player;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {

        if (!(object instanceof Goal)) {
            return false;
        }
        Goal other = (Goal) object;

        if ((this.id == null && other.id == null) && (this.scoreTime
                != other.scoreTime)) {

            if (!isEqual(this.player, other.player)) {
                return false;
            }

            if (!isEqual(this.scoreTime, other.scoreTime)) {
                return false;
            }

            if (!isEqual(this.soccerMatch, other.soccerMatch)) {
                return false;
            }
        }

        if ((this.id == null && other.id != null) || (this.id != null
                && !this.id.equals(other.id))) {
            return false;
        }

        return true;
    }

    private static boolean isEqual(Object expected, Object actual) {
        if ((expected == null && actual != null) || (expected != null && actual
                == null)) {
            return false;
        }

        if (expected != null && !expected.equals(actual)) {
            return false;
        }

        return true;
    }

    @Override
    public String toString() {
        return "cz.muni.fi.pa165.my_persistence_example.persistence.model.Goal[ id="
                + id + " ]";
    }

}
